const func = {
  // 日期格式化
  // fmt 'yyyy-MM-dd hh:mm:ss'
  formatDate(date, fmt) {
    if(!date) return ''
    date = new Date(date);
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }
    let o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds()
    };
    for (let k in o) {
      if (new RegExp(`(${k})`).test(fmt)) {
        let str = o[k] + '';
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : this.padLeftZero(str));
      }
    }
    return fmt;
  },
  
  padLeftZero(str) {
    return ('00' + str).substr(str.length);
  },
  
  // 计算几天前的日期
  InitDateTime(day) {
    const nowDate = new Date();
    const agoDate = new Date(nowDate);
    const nowDay = nowDate.getDate();

    day = -day + nowDay;
    agoDate.setDate(day);

    const dateFrom = this.formatDate(agoDate, 'yyyy-MM-dd');
    return dateFrom;
  },
  
  getQueryString(name) {
    // 获取哈希后面的URL部分
    const urlPart = window.location.hash ? window.location.hash : window.location.search;
    // 解码并匹配正则表达式
    const reg = new RegExp(`(?:^|&|\\?)${name}=([^&]*)(&|$)`, 'i');
    const r = decodeURIComponent(urlPart.substr(1)).match(reg);

    if (r !== null) {
      return decodeURIComponent(r[1]);
    }
    return null;
  },
  
  hideMobile(mobile) {
    if (!mobile || !mobile.trim()) return '';
    let returnData = mobile.substr(0, 3) + '****' + mobile.substr(-4, 4);
    return returnData;
  }
};

export default func;